﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using AjaxControlToolkit;
using com.paypal.soap.api;
using System.Data;
using System.Web.Security;
using ASPDotNetSamples;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Mail;

public partial class UserControl_Default_Email : System.Web.UI.UserControl
{

    private int DealID;
    public int _DealID
    {
        get
        {
            return DealID;
        }
        set
        {
            DealID = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Request.QueryString["DealID"] != null)
            {
                DealID = Convert.ToInt32(Request.QueryString["DealID"]);
            }
            BindRecord(DealID);

        }
        catch (Exception ex)
        {
        }

    }

    protected void linkrefferFriend_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ReferFriend.aspx");
    }
    protected override void OnPreRender(EventArgs e)
    {
        double Second = 0.0;
        try
        {

            if (ViewState["endDT"] != null && ViewState["starDT"] != null)
            {
                Second = System.Data.Linq.SqlClient.SqlMethods.DateDiffSecond(Convert.ToDateTime(System.DateTime.Now), Convert.ToDateTime(ViewState["endDT"]));
                if (Second <= 0)
                {
                    if (Session["RecentDealID"] == null)
                    {
                        timeCountDown.Visible = false;
                        //  Response.Redirect("NoDealExist.aspx");
                    }
                    else
                    {
                        timeCountDown.Visible = false;
                        Session["RecentDealID"] = null;
                    }
                }
                else
                {
                    hdnseconds.Value = Second.ToString();
                }
            }
            else
            {
                hdnseconds.Value = Second.ToString();
                Response.Redirect("NoDealExist.aspx");
            }

        }
        catch
        {
            hdnseconds.Value = Second.ToString();
            Response.Redirect("NoDealExist.aspx");
        }

    }
    public void RadAddressBox_ItemDataBound(object sender, RadListViewItemEventArgs e)
    {
        string address1 = RadAddressBox.DataKeyValues[0]["Address1"].ToString();
        string City = RadAddressBox.DataKeyValues[0]["City"].ToString();
        string State = RadAddressBox.DataKeyValues[0]["State"].ToString();
        UserControl Googlemap1 = (UserControl)e.Item.FindControl("Googlemap1");

    }

    private void BindRecord(int DealID)
    {
        try
        {

            ViewState["endDT"] = null;
            ViewState["starDT"] = null;
            var deal = new DealesDataContext();
            var query = deal.SelectDeal_ByDealID(Convert.ToInt32(DealID));
            foreach (var item in query)
            {
                Response.Cookies.Clear();
                Response.Cookies["CityID"].Value = Convert.ToString(item.DealCityID);
                Response.Cookies["CityID"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["CityName"].Value = item.city.ToString();
                Response.Cookies["CityName"].Expires = DateTime.Now.AddDays(30);
                lblVendor.Text = item.VendorName.ToString();
                lblDescription.Text = item.Description.ToString();
                lblDealTitle.Text = item.Name;
                txtSubject.Text = item.Name;
                txtMessage.Text = item.Name;
                txtMessage.Text += "\r\r https://www.zupons.com/default.aspx/DealID=" + item.DealID.ToString();
                lblValue.Text = "$" + item.RegularPrice.ToString();
                lblRegularPrice.Text = "$" + item.SellPrice.ToString();
                lblDiscount.Text = item.Discount.ToString() + "%";
                lblSave.Text = "$" + (Convert.ToDouble(item.RegularPrice) - Convert.ToDouble(item.SellPrice)).ToString();
                DealImg.ImageUrl = "../Admin/ImageDisplay.aspx?DealID=" + item.DealID;
                string EndDate = Convert.ToDateTime(item.SellEndDate.ToString()).ToShortDateString();
                string EndTime = Convert.ToDateTime(item.SellEndTime.ToString()).ToShortTimeString();
                DateTime starDT = Convert.ToDateTime(System.DateTime.Now);
                DateTime endDT = Convert.ToDateTime(EndDate + " " + EndTime);
                ViewState["endDT"] = endDT;
                ViewState["starDT"] = starDT;
                txtFootPrint.Text = item.FootPrint.ToString();
                Session["DealID"] = item.DealID.ToString();
                Session["CityID"] = item.DealCityID.ToString();
                Session["VendorID"] = item.VendorID.ToString();
                ViewState["DealID"] = item.DealID.ToString();
                var VendorDetails = new DealesDataContext();
                Twiteer.HRef = "https://twitter.com/home?status=" + item.Name + "- https://www.zupons.com/default.aspx";
                FaceBook.HRef = "https://www.facebook.com/sharer.php?u=https://www.zupons.com/default.aspx?dealID=" + item.DealID.ToString();

                var Vendor = VendorDetails.Select_VendorByID(Convert.ToInt32(item.VendorID.ToString()));
                foreach (var items in Vendor)
                {
                    if (items.Address1.ToString() != "")
                        lblVendor.Text += "<br/>" + items.Address1.ToString() + "<br/>";
                    if (items.City.ToString() != "")
                        lblVendor.Text += items.City.ToString() + ", ";
                    if (items.State.ToString() != "")
                        lblVendor.Text += items.State.ToString() + " ";
                    if (items.ZipCode.ToString() != "")
                        lblVendor.Text += items.ZipCode.ToString();
                    if (items.Phone_Off_.ToString() != "")
                        lblVendor.Text += "<br/>" + items.Phone_Off_.ToString();
                    website.HRef = "http://" + items.Website.ToString();
                }
                var Oder = new DealesDataContext();
                var TotaLQty = Oder.Select_OrderByDealID(item.DealID);
                foreach (var items in TotaLQty)
                {

                    TotalPurchaseQty.Text = items.Total.ToString();
                    if (TotalPurchaseQty.Text != "")
                    {
                        TotalPurchaseQty.Text += " Bought";
                    }

                    if (Convert.ToInt32(items.Total) >= Convert.ToInt32(item.MaxQty))
                    {
                        ImageButton1.Visible = false;
                        ImageButton2.Visible = true;
                    }
                }
                var DealAddress = new DealesDataContext();
                var DealAddressList = DealAddress.Select_DealAddressByDealID(Convert.ToInt32(item.DealID.ToString()));
                RadAddressBox.DataSource = DealAddressList;
                RadAddressBox.DataBind();
                if (RadAddressBox.Items.Count > 0)
                    Locations.Visible = true;
                else
                    Locations.Visible = false;

                var HighLights = DealAddress.Select_HighLight(Convert.ToInt32(item.DealID.ToString()));
                foreach (var items in HighLights)
                {
                    lblHighlights.Text += "<li>" + items.HighLights.ToString() + "</li>";
                }


            }


        }
        catch
        {
        }
    }
    protected void imgClose_Click(object sender, ImageClickEventArgs e)
    {
        Session["SubscribeDaialyDeal"] = null;
        divSubcrigePopUp.Attributes["style"] = "disply:none";
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        // MakeSSLSecure();
        Response.Redirect("OrderDetailNew.aspx?DealID=" + ViewState["DealID"].ToString());
        //Response.Redirect(Session["HttpsPath"].ToString() + "/OrderDetailNew.aspx?DealID=" + ViewState["DealID"].ToString());

    }
    protected string TrimDescription(string Address, string City, string State, string zipcode)
    {
        string add = Address + " " + City + " " + State + " " + zipcode;
        add = add.Replace(" ", "+");
        add += "&mrt=yp";
        return add;
    }
    protected string TrimDescription(string description)
    {
        if (!string.IsNullOrEmpty(description) && description.Length > 60)
        {
            return string.Concat(description.Substring(0, 60), "...");
        }
        return description;
    }
    protected void btnGiftSave_Click(object sender, EventArgs e)
    {
        //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
        //replacements.Add("<%To%>", txtTo.Text.ToString());
        //replacements.Add("<%From%>", txtFrom.Text.ToString());
        //replacements.Add("<%Subject%>", txtSubject.Text.ToString());
        //replacements.Add("<%Message%>", txtMessage.Text.ToString());

        //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl,
        //    Mailer.MessageTemplate.Email, replacements, txtTo.Text.ToString(), "Email from Zupons");
        //Mailer.InternalSend(confirmation, false);

        MailMessage Mail = new MailMessage();
        MailAddress ma = new MailAddress("mail@zupons.com", "Zupons");
        Mail.From = ma;
        Mail.To.Add(txtTo.Text.ToString());
        Mail.Subject = "Email from Zupons";
        Mail.Body = txtMessage.Text;
        Mail.IsBodyHtml = true;
        try
        {
            SmtpClient smtpMailObj = new SmtpClient();
            smtpMailObj.Host = "mail.zupons.com";
            smtpMailObj.Send(Mail);
            // Response.Write("Your Message has been sent successfully");
        }
        catch (Exception ex)
        {
            // Response.Write("Message Delivery Fails");
        }
    }
}
